// lives/real/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    getApp().page.onLoad(this, options);
    this.getLiceConfig();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    getApp().page.onShow(this);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 获取直播配置
  getLiceConfig: function () {
    var that = this;
    getApp().request({
      url: getApp().api.live.get_config,
      method: "post",
      success: function (t) {
        if (t.code == 0) {
          that.setData({
            user: t.data.user_info,
            config: t.data.config
          })
        }
      },
      complete: function (t) {
        getApp().core.hideLoading();
      }
    })
  },
  chooseImageZheng: function () {
    var that = this
    wx.chooseImage({
      sourceType: ['album', 'camera'],
      count: 1,
      success: function (res) {
        that.updloadImg(res.tempFilePaths,1)
      }
    })
  },
  chooseImageFan: function () {
    var that = this
    wx.chooseImage({
      sourceType: ['album', 'camera'],
      count: 1,
      success: function (res) {
        that.updloadImg(res.tempFilePaths,!1)
      }
    })
  },
  updloadImg: function (imageList,type) {
    wx.showLoading({
      title: '上传中',
    })
    var that = this;
    wx.uploadFile({
      url: getApp().api.default.upload_image,
      filePath: imageList[0],
      name: 'image',
      success: function (res) {
        var data_obj = JSON.parse(res.data);
        if (data_obj.code == 0) {
          var pic_img = data_obj.data.url;
          if (type) {
            that.setData({
              real_just_pic: pic_img
            })
          }else {
            that.setData({
              real_back_pic: pic_img
            })
          }
        } else {
          wx.showModal({
            title: '提示',
            content: res.msg
          })
        }

      },
      complete: function () {
        wx.hideLoading();
      }
    })
  },
  formSubmit: function (e) {
    var data = e.detail.value;
    var that = this;
    if (!data || !data.real_name || !data.real_code) {
      wx.showModal({
        title: '提示',
        content: '请补充完整内容',
        showCancel: false
      });
      return false;
    } else if (!this.data.real_just_pic && !this.data.user.real_just_pic){
      wx.showModal({
        title: '提示',
        content: '请上传正面身份证照片',
        showCancel: false
      });
      return false;
    } else if (!this.data.real_back_pic && !this.data.user.real_back_pic) {
      wx.showModal({
        title: '提示',
        content: '请上传反面身份证照片',
        showCancel: false
      });
      return false;
    } else {
      getApp().core.showLoading({
        title: "提交中"
      })
      var that = this;
      getApp().request({
        url: getApp().api.user.submit_real,
        method: "get",
        data: {
          real_name: data.real_name,
          real_code: data.real_code,
          real_back_pic: that.data.real_back_pic ? that.data.real_back_pic : user.real_back_pic,
          real_just_pic: that.data.real_just_pic ? that.data.real_just_pic : user.real_just_pic,
        },
        success: function (t) {
          if (t.code == 0) {
            wx.showModal({
              title: '提示',
              content: '提交成功',
              showCancel: !1,
              success: function (res) {
                if (res.confirm) {
                  getApp().core.switchTab({
                    url: "/pages/user/user"
                  });
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          } else {
            wx.showModal({
              title: '提示',
              content: t.msg,
              showCancel: false
            });
          }
        },
        complete: function (t) {
          getApp().core.hideLoading();
        }
      })
    }
  },
})